#include "stdio.h"

#define PERSON struct person
#define CARD   struct card
#define BOOK   struct book

#define NAME_LEN 16
#define PHON_LEN 20
#define ADDR_LEN 50
#define CODE_LEN 10


PERSON
{
 char name[NAME_LEN];
 char phon[PHON_LEN];
 char addr[ADDR_LEN];
 char code[CODE_LEN];
};

CARD
{
 PERSON person;
 CARD  *prev;
 CARD  *next;
};

BOOK
{
 int count;
 CARD *head;
 CARD *last;
 CARD *curcard;
};

void Person_init(PERSON *person,char *name,char *phon,char *addr,char *code)
{
 strcpy(person->name,name);
 strcpy(person->phon,phon);
 strcpy(person->addr,addr);
 strcpy(person->code,code);
}

void Card_init(CARD *card,PERSON *person)
{
 strcpy(card->person.name,person->name);
 strcpy(card->person.phon,person->phon);
 strcpy(card->person.addr,person->addr);
 strcpy(card->person.code,person->code);
 card->prev=NULL;
 card->next=NULL;
}

void Book_init(BOOK *book)
{
 book->count=0;
 book->head=NULL;
 book->last=NULL;
 book->curcard=NULL;
}

void Book_add(BOOK *book,CARD *card)
{
 if(book->count==0)
   {
	book->head=card;
	book->last=card;
	card->prev=NULL;
	card->next=NULL;
   }
 else
   {
	card->prev=book->last;
	card->next=NULL;
	book->last->next=card;
	book->last=card;
   }
 book->curcard=card;
 book->count++;
}

void Book_insert(BOOK *book,CARD *card)
{
 if(book->count==0)
   {
	Book_add(book,card);
	return;
   }
 else
 {
  card->next=book->curcard;
  card->prev=book->curcard->prev;
  book->curcard->prev=card;
  if(card->prev==NULL)
  {
   book->head=card;
  }
  else
  {
   card->prev->next=card;
  }
 }
 book->curcard=card;
 book->count++;
}

void Book_delete(BOOK *book)
{
 CARD *del;
 if(book->count==0)
   return;
 del=book->curcard;
 if(del->prev!=NULL)
   del->prev->next=del->next;
 if(del->next!=NULL)
   del->next->prev=del->prev;
 if(del==book->head)
   book->head=del->next;
 if(del==book->last)
   book->last=del->prev;
 if(del->next!=NULL)
   book->curcard=del->next;
 else
   book->curcard=del->prev;
 free(del);
 book->count--;
}

void Book_next(BOOK *book)
{
 if(book->curcard==NULL)
   return;
 if(book->curcard->next!=NULL)
   book->curcard=book->curcard->next;
}

void Book_prev(BOOK *book)
{
 if(book->curcard==NULL)
   return;
 if(book->curcard->prev!=NULL)
   book->curcard=book->curcard->prev;
}

void Book_first(BOOK *book)
{
 book->curcard=book->head;
}
void Book_last(BOOK *book)
{
 book->curcard=book->last;
}














